<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\datacache;

use think\facade\Cache;
use think\facade\Debug;
/**
 * 视频缓存层
 * @package app\user_center\model
 */
class VideoPlayPush{

    /**
     * 首页推荐 推送(游客随机推送)
     * @param uid int 用户id
     * @param cache 是否更新 缓存
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    public function getTouristPushList($num, $class_id = 0, $play_type, $cache = false){
        $cacheName = 'getIndexOnePushList_'.$class_id.'_'.$play_type;//dump(!Cache::get($cacheName));

        $cache_list = Cache::get($cacheName);//缓存池
        if(!$cache_list || $cache || count($cache_list) <= $num*2){
            $list = $this -> getIndexOnePushList($num, $class_id = 0, $play_type, $cacheName);
            $rel_list = $list['data'];
        }else{
            $this -> getIndexOnePushList($num1 = 3, $class_id = 0, $play_type, $cacheName);//新增三个缓存
            $list = Cache::get($cacheName);
            $temp_list = array_rand($list, $num);//随机取出10个
            
            foreach ($temp_list as $key => $value) {
                $rel_list[] = $list[$value];
            }
        }
        
        return array_out(200, 'success', $rel_list);
    }
    /**
     * 首页推荐 推送(游客随机推送)
     * @param uid int 用户id
     * @param cache 是否更新 缓存
     * @author jason wang <120907037@qq.com>
     * @return array
     */
    private function getIndexOnePushList($num, $class_id = 0, $play_type, $cacheName){
        //查询分类下所有子分类
        $map = $class_id_arr =[];
        $map['pid'] = $class_id;
        $list_class = model('VideoPlayClass') -> where($map) -> select()->toArray();
        foreach ($list_class as $key => $value) {
            $class_id_arr[] = $value['id'];
        }
        $class_id_str = implode(',', $class_id_arr);

        //拉取最新1000条
        $map = [];
        $map[] = ['play_operation_status', '=', 1];
		$map[] = ['play_status', '=', 1];//视频状态 0禁用 1启用 2测试可见
        $map[] = ['play_type', '=', $play_type];
        if($class_id > 0) $map[] = ['video_classid', 'in', $class_id_str];
        $list = model('VideoPlay')->where($map) -> order('create_time desc')->cache(true, 60*60*24)  -> limit(10000) -> select();
        if(!$list) return array_out(200, 'success', []);
        $list = $list -> toArray();
        
        $num = count($list) <= $num ? count($list) : $num;
        if($num > 1){
            $rel_list = array_rand($list,$num);
            $i = 0;
            foreach ($rel_list as $key => $value) {
                $value_list[] = $list[$value];
                $i++;
                if($i == $num) break;
            }
        }else{
            $value_list = $list;
        }
        
        //标准输出
        $result_list = controller('Videologic', 'logic')->returnBaceArray($uid=0, $value_list);
        
        //缓存压缩
        if(!Cache::get($cacheName) || count(Cache::get($cacheName)) >= 10000){
            Cache::set($cacheName, $result_list, 60*60*24*30);//默认缓存30天
        }else{
            $cache_list = Cache::get($cacheName);
            $merge_arr = array_merge($cache_list, $result_list);
            $rel_list = array_unset_tt($merge_arr, $key='playId');//去重
            Cache::set($cacheName, $rel_list, 60*60*24*30);//默认缓存30天 
        }
        return array_out(200, 'success', $result_list);
    }

    
}
